package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/17 0017.
 */
@Controller
@RequestMapping(ControllerConstants.USER)
public class UserWController extends WebBaseController {

    @Autowired
    private UserService userService;

    /**
     * 调转用户页面
     *
     * @return
     */
    @RequestMapping("/findUserIndex")
    public String fingIndex() {
        return "taoshunda/user/user";
    }

    /**
     * 进行统计页面的跳转
     */

    @RequestMapping("index")
    public String index() {
        return "taoshunda/statistics/index";
    }

    /**
     * 查询用户列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/ queryUserList")
    public void queryUserList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                String nickName = getString(request, "name", "");
                String startCreate = getString(request, "startCreate", "");
                String endCreate = getString(request, "endCreate", "");
                String startTime = getString(request, "startTime", "");
                String endTime = getString(request, "endTime", "");
                return userService.queryUserList(nowPage, pageSize, nickName, startCreate, endCreate, startTime, endTime);
            }
        });
    }

    /**
     * 查询订单列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryOrderList")
    public void queryOrderList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return userService.queryOrderList(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询关联商家列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryCompanyList")
    public void queryCompanyList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return userService.queryCompanyList(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询关联商家订单列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryCompanyOrderList")
    public void queryCompanyOrderList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return userService.queryCompanyOrderList(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询收支明细
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryMoneyDetails")
    public void queryMoneyDetails(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return userService.queryMoneyDetails(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 修改用户信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateUserInfo")
    public void updateUserInfo(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String account = getString(request, "account", "");
                String nickName = getString(request, "nickName", "");
                String headPic = getString(request, "headPic", "");
                return userService.updateUserInfo(account, nickName, headPic, id);
            }
        });
    }

    /**
     * 重置密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String pwd = getString(request, "pwd", "");
                return userService.resetPassword(id, pwd);
            }
        });
    }

    /**
     * 删除、启用、禁用
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateStatus")
    public void updateStatus(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "state", 0);
                return userService.updateUserState(id, state);
            }
        });
    }

    /**
     * 进行数据的统计
     */
    @RequestMapping("/statistics")
    public void statistics(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return userService.statistics();
            }
        });
    }

    /**
     * 进行数据的统计
     */
    @RequestMapping("/pass")
    public void pass(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(httpServletRequest, "id", 0);
                Integer type = getInt(httpServletRequest, "type", 1);
                return userService.pass(id, type);
            }
        });
    }
}
